package src.list;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author starsea
 * @date 2024-08-16 10:32
 */

public class StackByQueue {
    Queue<Integer> queue1;
    Queue<Integer> queue2;

    public StackByQueue() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();

    }

    public void push(int x) {
        queue2.offer(x);
        while (!queue1.isEmpty()) {
            queue2.offer(queue1.poll());
        }
        Queue<Integer> tmp = queue1;
        queue1 = queue2;
        queue2 = tmp;
    }

    public int pop() {
        if (queue1.isEmpty()) {
            return -1;
        }
        return queue1.poll();


    }

    public int top() {
        if (queue1.isEmpty()) {
            return -1;
        }
        return queue1.peek();
    }

    public boolean empty() {
        return queue1.isEmpty();
    }
}

